home *** CD-ROM | disk | FTP | other *** search
/ boe.pres.k12.wv.us / boe.pres.k12.wv.us.zip / boe.pres.k12.wv.us / Utilities / Xerox Workcentre 5335 / Windows Scan / 64-bit_x64 / Russian / cpsimage.cab / data / xps / xmetWriteXPS.proc < prev    next >
Text File  |  2009-04-23  |  7KB  |  208 lines

  1. /* What this script for:
  2.    Reads in a directory name and gets a list of IMS files from the directory
  3.    to package into a XPS file.
  4.    The files in the directory are assumed to have FIJI segmentation already
  5.    done. Will take the mask layers and assemble a page to do the OCR on.
  6.    If no mask layers are present it will use layer 1's contone data to do
  7.    the OCR on.
  8. */
  9.  
  10. // Load scripted support procedures
  11.  
  12. #load "sys/stdlib.elf";
  13. #load "xps/xmetWriteImg.proc";        /* XPS support procs */
  14. #load "xps/xmetWriteXML.proc";        /* XPS support procs */
  15. #load "sys/zip.elf";                  /* ZIP support objects */
  16. #load "docio/xmldocutils.elf";        /* XIPXML_COALESCEWORDS support */
  17. #import "documentio.ucm";             /* for: XIPXMLtoXPSXML */
  18. #import "builtins.ucm";               /* for: GetDataResource */ 
  19.  
  20. /* @WriteXPSOpen
  21.   // DESCRIPTION
  22.   Deletes an existing file of filename. Creates a temporary directory of
  23.   directory of "filename".tmp and sets up the associated sub directories
  24.   for the XPS directory structure and write the XPS pages that are not
  25.   dependent on image pages.
  26. */
  27. PROCEDURE
  28. WriteXPSOpen (STRING filename)            /* XPS full path name */
  29. {
  30.  
  31.    STRING outdir;
  32.    FILE file;
  33.  
  34.    if (filename) {
  35.       file = new (FILE, path:filename);
  36.       if (file.exists()) {
  37.         // SetStatus ( op: "stop", msg: "filename " + filename + " already exists\n");
  38.         file.deleteFile();
  39.       }
  40.    }
  41.  
  42.    outdir = filename + "tmp";
  43.    file = new (FILE, path: outdir);
  44.    if ( file.isDirectory() ) {
  45.      // SetStatus ( op: "stop", msg: "tempdir " + outdir + " already exists\n");
  46.      file.deleteFiles();
  47.    }
  48.    
  49.    new (FILE, path:outdir).mkdir();
  50.    new (FILE, path:outdir + "/_rels").mkdir();
  51.    new (FILE, path:outdir + "/Images").mkdir();
  52.    new (FILE, path:outdir + "/Fonts").mkdir();
  53.    
  54.    // XML pages not dependent on pages
  55.    WriteDocSeq (dir: outdir);
  56.    WriteFixedDocHdr (dir: outdir);
  57. }
  58.  
  59.  
  60. PROCEDURE
  61. WriteXPSPage(  STRING filename,         /* XPS full path name */
  62.                XIPIMAGE img,            /* input image */
  63.                INTEGER  pgNum,          /* page number */
  64.                DOUBLE page_x,           /* custom page size im mm - default = 0 */
  65.                DOUBLE page_y,           /* custom page size im mm - default = 0 */
  66.                INTEGER unit,            /* Resolution unit, 1 = inches, 2 = mm */
  67.                INTEGER isTIFF,          /* tif or png */
  68.                BOOLEAN doCoalesce)      /* coalesce ocr'd words in sentences */
  69. {
  70.  
  71.     // Importable value for variable "dir" and "image"
  72.     STRING outdir, reldir, thumbName, ocrxml, xipocr;
  73.     XIPIMAGE thumb;
  74.     INTEGER i, ltype;
  75.  
  76.     outdir = filename + "tmp";
  77.     reldir = outdir + "/_rels";
  78.     STRING fontpath, myfont, getFontLoc; 
  79.     FILE   file;
  80.  
  81.     for ( i=0; i<img.nlayers; i++ ) {
  82.         WriteLayer (img: img,
  83.                     dir: outdir,
  84.                   fname: "Layer",
  85.                   pgNum: pgNum,
  86.                layerNum: i,
  87.                isTIFF: isTIFF);
  88.     }
  89.  
  90.     for ( i=0; i<img.nlayers; i++ )
  91.     {
  92.         ltype = img.getMember (member:"layerType", num: i);
  93.         
  94.         if (ltype == XIP_Text ) {
  95.             DOUBLE     fx = 96 / img.resolution[0];
  96.             DOUBLE     fy = 96 / img.resolution[1];
  97.             DOUBLE     ww = img.getMember (num:i, member:"width")  * fx;
  98.             DOUBLE     hh = img.getMember (num:i, member:"height") * fy;
  99.  
  100.             XIPIMAGE layer = img.getLayer (num: i);
  101.             INTEGER  txtfmt = layer.getMember (member: "textformat");
  102.           
  103.             // if ( txtfmt == XIP_XPS_TEXT ) {
  104.                // ocrxml = layer.getMember(member:"text");
  105.                // continue;
  106.             // }
  107.             if ( txtfmt != XIP_XML_TEXT ) {
  108.                print "Invalid text layer format type to include in XPS";
  109.                continue;
  110.             }
  111.  
  112.             xipocr = layer.getMember(member:"text");
  113.  
  114.             if (doCoalesce) {
  115.                /* May need to override the default tolerences some day */
  116.                xipocr = XIPXML_COALESCEWORDS.format( xipxml:xipocr );
  117.             }
  118.  
  119.             XIPXMLtoXPSXML(xipxml:xipocr, width:ww, height:hh)
  120.                Returns (xpsxml: ocrxml,  font: myfont);
  121.             fontpath = outdir + "/Fonts/" + myfont;
  122.             file = new (FILE, path: fontpath);
  123.             if ( myfont && ! file.isFile () ) {
  124.                getFontLoc = GetDataResource(filename: "xps/Fonts/"+myfont) ;     
  125.                System (cmd: "cp \"" + getFontLoc + "\" \"" + fontpath + "\"");
  126.             }
  127.         } else if (ltype == XIP_Thumbnail) {
  128.             thumb = img.getLayer (num: i);
  129.         }
  130.     }
  131.  
  132.     // Write XML XPS support files
  133.     WriteContentTypes ( dir: outdir, page: pgNum);
  134.     WriteFixedPg (img: img,
  135.                   dir: outdir,
  136.                  page: pgNum,
  137.                ocrxml: ocrxml,
  138.                page_x: page_x,
  139.                page_y: page_y,
  140.                  unit: unit,
  141.                isTIFF: isTIFF);
  142.     WriteFixedDoc ( dir: outdir, page: pgNum);
  143.  
  144.     if (thumb) {
  145.         thumbName = "/Images/pg" + pgNum + "_thumb.jpg";
  146.         // The delete was put in place to avoid changing checksums when units were allowed again
  147.         thumb.modheader(_delete:("Origin:units")).writejpgop ( filename: outdir + thumbName);
  148.     } else {
  149.         thumbName = "";
  150.     }
  151.     if (pgNum == 1) {
  152.         WriteRelsDotRels (dir: reldir, thumbName: thumbName);
  153.     }
  154.     WriteRelsFixedPg (img: img,
  155.                       dir: reldir,
  156.                      page: pgNum,
  157.                 thumbName: thumbName,
  158.                      font: myfont,
  159.                    isTIFF: isTIFF);
  160.     Execute ();
  161. }
  162.  
  163.  
  164.  
  165. PROCEDURE
  166. WriteXPSClose (STRING filename, 
  167.                INTEGER security,
  168.                STRING creator,
  169.                STRING title,
  170.                STRING keyword,
  171.                STRING dateTime)
  172. {
  173.    FILE outxps = new (FILE, path:filename);
  174.    FILE outxpsdir = new (FILE, path:filename + "tmp" );
  175.    STRING outdir, reldir;
  176.  
  177.    if (!outxps.getExt() ) {
  178.       outxps = new (FILE, parent:outxps.getParentFile(), path:outxps.getName() + ".XPS");
  179.    }
  180.  
  181.    outdir = new (FILE, path: filename + "tmp").getAbsolutePath();
  182.    reldir = outdir + "/_rels";
  183.  
  184.    WriteFixedDocEnd ( dir: outdir );
  185.    WriteMetaData ( dir: outdir,
  186.                creator: creator,
  187.                  title: title,
  188.                keyword: keyword,
  189.               dateTime: dateTime);
  190.    WriteDocSeqRels (dir: reldir);
  191.  
  192.  
  193.    /* Zip it up!  */
  194.    ZIP zip = new (ZIP, file:outxps);
  195.    zip.setOption( option:ZIP.NODIRS );         // Don't store directory references
  196.  
  197.    // Are we regression testing
  198.    if ( dateTime.match ( str: "2007-01-01T" ) )
  199.       zip.setOption( option:ZIP.NOTIME );         // Regression testing to remove timestamp 
  200.  
  201.    zip.addFiles( dir:outxpsdir, recurse:TRUE );
  202.    zip.close();
  203.  
  204.    if (!security) {
  205.       outxpsdir.deleteFiles( recurse:TRUE );
  206.    }
  207. }
  208.